/**
 * @date 3:41:34 PM
 * SMSAPIServiceImpl.java
 * Administrator
 * TODO
 */
package com.qs.education.service.impl;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qs.education.bean.api.PageBean;
import com.qs.education.bean.api.ResponseBean;
import com.qs.education.bean.beans.EducationSMSResponse;
import com.qs.education.bean.entity.EducationSMS;
import com.qs.education.bean.enums.ResponseStatus;
import com.qs.education.bean.service.BaseService;
import com.qs.education.service.ISMSAPIService;

/**
 * @class SMSAPIServiceImpl	
 * @author Warren
 * @description
 * @date Nov 5, 2018 
 */
@Service
public class SMSAPIServiceImpl extends BaseService implements ISMSAPIService{

	/**
	 * @author Warren
	 * @return EducationSMSResponse
	 * @description 给用户发送短信
	 * @date Nov 5, 2018
	 */
	@Override
	public EducationSMSResponse getSMS(EducationSMS obj) {
		//创建返回对象
		EducationSMSResponse reObj = new EducationSMSResponse();
		//获得短信验证码
		int code = (int)((Math.random()*9+1)*1000);
		obj.setCode(String.valueOf(code));
		obj.setSendTime(String.valueOf(System.currentTimeMillis()));
		boolean result = iEducationSMSDao.saveEducationSMS(obj);
		//根据后台保存情况给用户发送短信
		if(result) {
			//保存成功，此处引入短信发送接口
			boolean smsResult = true;
			//根据短信业务的发送结果设置EducationSMS的状态
			if(smsResult) {
				//短信发送成功
				obj.setSmsStatus(1);
				iEducationSMSDao.update(obj);
				reObj.setStatus(ResponseStatus.SUCCESS.toString());
				reObj.setMessage("短信获取成功！");
			}else {
				//短信发送失败
				obj.setSmsStatus(0);
				iEducationSMSDao.update(obj);
				reObj.setStatus(ResponseStatus.FAILED.toString());
				reObj.setMessage("短信获取失败，重新获取！");
			}
		}else {
			//保存失败
			reObj.setStatus(ResponseStatus.FAILED.toString());
			reObj.setMessage("短信获取失败，重新获取！");
			return reObj;
		}
	
		
		if(obj!= null) {
			BeanUtils.copyProperties(obj, reObj);
		}
		return reObj;
	}

	/**
	 * @author Warren
	 * @param mobile 手机号
	 * @param code 验证码
	 * @return ResponseBean
	 * @description 用户发送的验证码与后台存储的进行校验
	 * @date Nov 5, 2018
	 */
	@Override
	public ResponseBean checkSMS(String mobile, String code) {
		EducationSMS obj = iEducationSMSDao.findEducationSMSByMobile(mobile);
		ResponseBean responseBean = new ResponseBean();
		if(obj != null) {
			Long sendTime = Long.valueOf(obj.getSendTime());
			if(System.currentTimeMillis()-sendTime>1000*60*5) {
				responseBean.setStatus(ResponseStatus.FAILED.toString());
				return responseBean;
			}
			
			if(!obj.getCode().equals(code)) {
				responseBean.setStatus(ResponseStatus.FAILED.toString());
				return responseBean;
			}
			
			responseBean.setStatus(ResponseStatus.SUCCESS.toString());
			return responseBean;
		}
		
		responseBean.setStatus(ResponseStatus.FAILED.toString());
		return responseBean;
	}

	@Override
	public PageBean getSMSList(EducationSMS educationSMS, Integer currentPage, Integer pageSize) {
		
		if (educationSMS.getMobile()==null || educationSMS.getMobile().trim().equals("")) {
			educationSMS.setMobile("%%");
		}
		int recordCount = iEducationSMSDao.getSMSCount(educationSMS.getMobile());
		List<EducationSMS> list = iEducationSMSDao.getSMSList(educationSMS.getMobile(), currentPage, pageSize);
		for (EducationSMS sms :list) {
			sms.setSendTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(Long.parseLong(sms.getSendTime()))));
		}
		PageBean pagePean = new PageBean(currentPage, pageSize, recordCount, list);
		
		return pagePean;
	}

	@Override
	@Transactional
	public Boolean deleteSMSById(Integer id) {
		
		return iEducationSMSDao.delete(id, EducationSMS.class);
	}

}
